<template>
    <div class="app-container shop">
        <!--<el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="用户ID" prop="uid">
        <el-input
          v-model="queryParams.uid"
          placeholder="请输入用户ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属平台" prop="plat">
        <el-input
          v-model="queryParams.plat"
          placeholder="请输入所属平台"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="店铺名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入店铺名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="店铺key" prop="appKey">
        <el-input
          v-model="queryParams.appKey"
          placeholder="请输入店铺key"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="店铺secret" prop="appSecret">
        <el-input
          v-model="queryParams.appSecret"
          placeholder="请输入店铺secret"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="贸易形式" prop="tradefrom">
        <el-input
          v-model="queryParams.tradefrom"
          placeholder="请输入贸易形式"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="经营类目" prop="jycategory">
        <el-input
          v-model="queryParams.jycategory"
          placeholder="请输入经营类目"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="经营品牌" prop="jypipai">
        <el-input
          v-model="queryParams.jypipai"
          placeholder="请输入经营品牌"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createdTime">
        <el-date-picker clearable size="small" style="width: 200px"
          v-model="queryParams.createdTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="状态  0 正常 1不正常" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态  0 正常 1不正常" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
     -->
        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['biz:shop:add']">新增</el-button>
            </el-col>
            <!--   <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['biz:shop:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['biz:sh   op:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['biz:shop:export']"
        >导出</el-button></el-col> -->
        </el-row>

        <el-table v-loading="loading" :data="shopList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <!--<el-table-column label="状态  0 正常 1不正常" align="center" prop="id" />
      <el-table-column label="用户ID" align="center" prop="uid" />-->
            <el-table-column label="所属平台" align="center" prop="platName" />
            <el-table-column label="店铺名称" align="center" prop="name" />
            <!-- <el-table-column label="店铺key" align="center" prop="appKey" />
      <el-table-column label="店铺secret" align="center" prop="appSecret" /> -->
            <el-table-column label="贸易形式" align="center" prop="tradefrom" />
            <el-table-column label="经营类目" align="center">
                <template slot-scope="scope">
                    <el-popover placement="top-start" width="400" trigger="hover" :content="scope.row.jycategory">
                        <span class="spans" slot="reference">{{ scope.row.jycategory }}</span>
                    </el-popover>
                </template>
            </el-table-column>
            <el-table-column label="经营品牌" align="center" prop="jypipai" />
            <el-table-column label="创建时间" align="center" prop="createdTime" width="180">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.createdTime, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>

            <!-- <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat" /> -->
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['biz:shop:edit']">修改</el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['biz:shop:remove']">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

        <!-- 添加或修改店铺对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="100px">
                <!-- <el-form-item label="用户ID" prop="uid">
          <el-input v-model="form.uid" placeholder="请输入用户ID" />
        </el-form-item>-->
                <div class="flex">
                    <el-form-item label="所属平台" prop="plat">
                        <el-select v-model="form.plat" placeholder="请输入所属平台" @change="platSelect">
                            <el-option v-for="dict in platOptions" :key="dict.id" :label="dict.name" :value="dict.id"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="店铺名称" prop="name">
                        <el-input v-model="form.name" placeholder="请输入店铺名称" />
                    </el-form-item>
                </div>
                <div class="flex">
                    <el-form-item label="店铺key" prop="appKey" v-show="keyshow">
                        <el-input v-model="form.appKey" :show-password="isPassword" placeholder="请输入店铺key" :disabled="disabled" />
                    </el-form-item>
                    <el-form-item label="店铺secret" prop="appSecret" v-show="keyshow">
                        <el-input v-model="form.appSecret" :show-password="isPassword" placeholder="请输入店铺secret" :disabled="disabled" />
                    </el-form-item>
                </div>
                <div class="flex">
                    <el-form-item label="店铺token" prop="appToken" v-show="tokenshow">
                        <el-input v-model="form.appToken" placeholder="考拉店铺授权token" />
                    </el-form-item>

                    <el-form-item label="贸易形式" prop="tradefrom">
                        <el-select v-model="form.tradefrom" placeholder="请输入贸易形式">
                            <el-option v-for="(item, index) in maoyiOptions" :key="index" :label="item" :value="index"></el-option>
                        </el-select>
                    </el-form-item>
                </div>
                <div class="flex">
                    <el-form-item label="经营品牌" prop="jypipai">
                        <el-select class="select" v-model="jypipaiList" multiple filterable default-first-option placeholder="请选择品牌标签" @change="handelChange">
                            <el-option v-for="item in brandOptions" :key="item.brandNo" :label="item.brandName" :value="item.brandNo"> </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="经营类目" prop="jycategory">
                        <el-cascader v-model="form.jycategory" :options="options" :props="optionProps" :show-all-levels="false" collapse-tags clearable></el-cascader>
                    </el-form-item>
                </div>
                <div style="margin-left:25px">
                    <el-checkbox v-model="checked">
                        <span>请勾选</span>
                        <span @click="getAgreement()" style="color:#409EFF;">《电子会员协议》</span>
                    </el-checkbox>
                </div>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm" :disabled="!checked">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
        <el-dialog title="电子会员协议" :visible.sync="dialogVisible" width="60%">
            <span v-html="getAgreementCon"></span>
            <span slot="footer" class="dialog-footer">
                <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { listShop, getShop, delShop, addShop, updateShop, exportShop } from '@/api/biz/shop';
import { Agreements2 } from '@/api/biz/agreements.js';

export default {
    name: 'Shop',
    data() {
        return {
            checked: false,
            disabled: false,
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 总条数
            total: 0,
            // 店铺表格数据
            shopList: [],
            // 弹出层标题
            title: '',
            // 状态字典
            statusOptions: [],
            jypipaiList: [],
            maoyiOptions: [],
            platOptions: [],
            catogeryOptions: [
                {
                    name: '婴幼儿服饰',
                    id: 'A01B01C01'
                },
                {
                    name: '儿童服饰',
                    id: 'A04B02C01'
                },
                {
                    name: '丝质领带/领结',
                    id: 'A02B02C01'
                },
                {
                    name: '男士皮质腰带',
                    id: 'A02B01C01'
                }
            ],
            brandOptions: [
                {
                    name: '选项一',
                    id: 'A02B02C01'
                },
                {
                    name: 'A02B01C01',
                    id: 2
                }
            ],
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                uid: undefined,
                plat: undefined,
                name: undefined,
                appKey: undefined,
                appSecret: undefined,
                tradefrom: undefined,
                jycategory: undefined,
                jypipai: undefined,
                createdTime: undefined,
                status: undefined
            },
            // 表单参数
            form: {
                jypipai: []
            },
            // 表单校验

            rules: {
                uid: [
                    {
                        required: true,
                        message: '用户ID不能为空',
                        trigger: 'blur'
                    }
                ],
                name: [
                    {
                        required: true,
                        message: '店铺名称不能为空',
                        trigger: 'blur'
                    }
                ],
                plat: [{ required: true, message: '请选择', trigger: 'blur' }],
                // appKey: [
                //   { required: true, message: "不能为空", trigger: "blur" }
                // ],
                // appSecret: [
                //   { required: true, message: "不能为空", trigger: "blur" }
                // ],
                tradefrom: [{ required: true, message: '请选择', trigger: 'blur' }],
                jycategory: [{ required: true, message: '请选择', trigger: 'blur' }],
                jypipai: [{ required: true, message: '请选择', trigger: 'blur' }]
            },
            //  经营类目

            optionProps: {
                value: 'id',
                label: 'name',
                children: 'children',
                multiple: true,
                emitPath: false
            },

            options: [],
            keyshow: true,
            tokenshow: true,
            isPassword: false,
            dialogVisible: false,
            getAgreementCon: ''
        };
    },
    created() {
        this.getList();
        this.getDicts('common_status').then(response => {
            this.statusOptions = response.data;
        });
        this.getUrl('/web/getPlatlist').then(response => {
            this.platOptions = response.data;
        });
        this.getUrl('/web/getCommonData').then(response => {
            this.options = this.getTreeData(response.data.catogery);
            this.brandOptions = response.data.brand;
            this.maoyiOptions = response.data.maoyi;
        });
    },
    methods: {
        handelChange() {
            this.form.jypipai = this.jypipaiList;
        },
        platSelect(e) {
            // if (e == 3) {
            //     this.form.appKey = '29dda1b460c95fad40786fb9c3d395ea';
            //     this.form.appSecret = 'f44f7774b30098b8ca55e482cad4948a143b6d4deedbd8bc5855d21369a1a231';
            //     this.disabled = true;
            //     this.isPassword = true;
            // } else {
                this.form.appKey = '';
                this.form.appSecret = '';
                this.disabled = false;
                this.isPassword = false;
            // }
        },
        getTreeData(data) {
            for (var i = 0; i < data.length; i++) {
                if (data[i].children.length < 1) {
                    data[i].children = undefined;
                } else {
                    this.getTreeData(data[i].children);
                }
            }
            return data;
        },
        /** 查询店铺列表 */
        getList() {
            this.loading = true;
            listShop(this.queryParams).then(response => {
                this.shopList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: undefined,
                uid: undefined,
                plat: undefined,
                name: undefined,
                appKey: undefined,
                appSecret: undefined,
                tradefrom: undefined,
                jycategory: [],
                jypipai: [],
                createdTime: undefined,
                status: '0'
            };
            this.resetForm('form');
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm('queryForm');
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id);
            this.single = selection.length != 1;
            this.multiple = !selection.length;
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.jypipaiList = [];
            this.form.jycategory = undefined;
            this.reset();
            this.open = true;
            this.title = '添加店铺';
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || this.ids;
            getShop(id).then(response => {
                this.form = response.data;
                this.form.tradefrom = Number(response.data.tradefrom);
                this.form.status = String(response.data.status);

                this.form.jycategory = response.data.jycategory.split(',');
                this.jypipaiList = response.data.jypipai.split(',');

                if (this.form.plat == 3) {
                    //this.form.appKey = '29dda1b460c95fad40786fb9c3d395ea';
                    //this.form.appSecret = 'f44f7774b30098b8ca55e482cad4948a143b6d4deedbd8bc5855d21369a1a231';
                    //this.disabled = true;
                }

                this.open = true;
                this.title = '修改店铺';
            });
        },
        // 状态字典翻译
        statusFormat(row, column) {
            return this.selectDictLabel(this.statusOptions, row.status);
        },
        /** 提交按钮 */
        submitForm: function() {
            this.$refs['form'].validate(valid => {
                if (valid) {
                    if (this.form.uid != undefined) {
                        updateShop(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess('修改成功');
                                this.open = false;
                                this.getList();
                                this.$store.state.user.shopids = '0';
                                console.log('$store.state.user.shopids' + this.$store.state.user.shopids);
                            } else {
                                this.msgError(response.msg);
                            }
                        });
                    } else {
                        addShop(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess('新增成功');
                                this.open = false;
                                this.getList();
                                this.$store.state.user.shopids = '1';
                                console.log('$store.state.user.shopids' + this.$store.state.user.shopids);
                            } else {
                                this.msgError(response.msg);
                            }
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$confirm('是否确认删除店铺编号为"' + ids + '"的数据项?', '警告', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
                .then(function() {
                    return delShop(ids);
                })
                .then(() => {
                    this.getList();
                    this.msgSuccess('删除成功');
                })
                .catch(function() {});
        },
        /** 导出按钮操作 */
        handleExport() {
            const queryParams = this.queryParams;
            this.$confirm('是否确认导出店铺数据项?', '警告', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
                .then(function() {
                    return exportShop(queryParams);
                })
                .then(response => {
                    this.download(response.msg);
                })
                .catch(function() {});
        },
        // 请求协议
        getAgreement() {
            Agreements2().then(res => {
                if (res.code == 200) {
                    this.getAgreementCon = res.data.content;
                    this.dialogVisible = true;
                }
            });
        }
    }
};
</script>
<style rel="stylesheet/scss" lang="scss">
.select {
    width: 300px;
}
.shop .spans {
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 3;
    overflow: hidden;
}
</style>
